package com.marvels.approve.process.listener;

import com.marvels.approve.common.utils.ApplicationContextAchieve;
import com.marvels.approve.process.dto.CompleteProcessDTO;
import com.marvels.approve.process.dto.ProcessApproveDTO;
import com.marvels.approve.process.service.IProcessService;
import com.marvels.approve.process.service.NodeFlowService;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.utils.MarvelsLogUtil;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.stereotype.Component;

/**
 * 审批通过监听
 * 
 * @author tangpw
 *
 */
@Component
public class ApprovalReviewListen implements ExecutionListener {
	
	
	private static final long serialVersionUID = 1L;

	
	
	public boolean judgeWalkingAudit(String loanId){
		boolean dto =ApplicationContextAchieve.getApplicationContext().getBean(NodeFlowService.class).selectViolateCode(loanId);
		return dto;
	}


	@Override
	public void notify(DelegateExecution execution) throws Exception {
		MarvelsLogUtil.getInstance().info(">>>>>>>>>>审批通过监听开始");
		TaskService taskService=ApplicationContextAchieve.getApplicationContext().getBean(TaskService.class);
		IProcessService processService=ApplicationContextAchieve.getApplicationContext().getBean(IProcessService.class);
		String processInstanceId = execution.getProcessInstanceId();
		TaskQuery tq = taskService.createTaskQuery().processInstanceId(processInstanceId);
		Task task = tq.singleResult();
		String curNodeCode = task.getTaskDefinitionKey();
		String curNodeName = task.getName();
		String curTaskId = task.getId();
		String loanId = "";
		Object object = taskService.getVariable(task.getId(),"loanId");
		if (null != object) {
			loanId =String.valueOf(object);
		}
		String orginProcessResult = (String)taskService.getVariable(task.getId(),"processResult");
		boolean isWalkingAudit=false;
		if (MarvelsConstant.PROCESS_RESULT_OK.equalsIgnoreCase(orginProcessResult)) {// 审批通过
			// 判断是否走审计
			try {
				isWalkingAudit = judgeWalkingAudit(loanId);
			} catch (Exception e) {
				MarvelsLogUtil.getInstance().error(">>>>>>>loanId" + loanId + ",审批通过监听判断是否走审计异常:", e);
				throw e;
			}
			CompleteProcessDTO proDto = new CompleteProcessDTO();
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>审批通过监听判断是否走审计curNodeCode:" + curNodeCode + ",curNodeName"
					+ curNodeName + ",curTaskId" + curTaskId);
//			ActivitiNodeDTO curNode = new ActivitiNodeDTO();
//			curNode.setNodeCode(CaseNodeConstant.WOEXCLUSIVEGATEWAY5.getCode());
			ProcessApproveDTO processApproveDto = new ProcessApproveDTO();
			// 走审计
			if (isWalkingAudit) {
				processApproveDto.setProcessResult(MarvelsConstant.PROCESS_RESULT_OK);
				// 设置当前节点
//				proDto.setCurNode(curNode);
			} else {//不走审计
				processApproveDto.setProcessResult(MarvelsConstant.PROCESS_RESULT_SKIP);
				// 设置当前节点
//				proDto.setCurNode(curNode);
			}
//			processApproveDto.setLoanId(loanId);
//			taskService.setVariables(task.getId(), BeanUtils.beanConverToMap(processApproveDto));
			taskService.setVariable(task.getId(), "processResult", processApproveDto.getProcessResult());
			taskService.setVariable(task.getId(), "loanId", loanId);
			MarvelsLogUtil.getInstance().info(">>>>>>>测试日志processApproveDtoprocessResultprocessResultprocessResultprocessResult:"+ taskService.getVariable(task.getId(),"processResult"));
//			try {
//				proDto.setProcessApproveDto(processApproveDto);
//				processService.completeLoanApplyCase(proDto);
//			} catch (MarvelsBusinessException e) {
//				MarvelsLogUtil.getInstance().error(">>>>>>>loanId" + loanId + ",征信3阶段监听扭转节点异常:", e);
//			}
		}
	}

}
